package com.zhao.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.zhao.pojo.Admin;
import com.zhao.pojo.Student;
import com.zhao.pojo.StudentImpl;
import com.zhao.service.DepartmentService;
import com.zhao.service.DrivingLicenseService;
import com.zhao.service.EbickService;
import com.zhao.service.StudentService;
import com.zhao.utils.XlxsTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Controller
public class StudentController {

    @Autowired
    private DrivingLicenseService drivingLicenseService;

    @Autowired
    private EbickService ebickService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private DepartmentService departmentService;

    ////////////////////////// index //////////////////////////////////////////



    /////////////////////////// admin ///////////////////////////////////////////////

    //////////////////待实现
    @ResponseBody
    @RequestMapping("upLoadStudentFile.do")
    public String upLoadStudentFile(MultipartFile file, HttpSession session) throws IOException {

        String filename = file.getOriginalFilename();
        System.out.println(filename);
        if (!(filename.endsWith(".xls")|| filename.endsWith(".xlsx"))){
            System.out.println("格式错误");
        }
        // 单 sheet 读取
        List<Student> dataList = XlxsTool.loadExcel(file.getInputStream(), Student.class);

        System.out.println(dataList);

        return "";
    }

    @ResponseBody
    @RequestMapping("admin-seeOneStudent.do")
    public String adminSeeOneStudent(HttpSession session,String studentID){
        Admin admin = (Admin)session.getAttribute("admin");
        if(admin == null){
            return "redirect:/adminLogin";
        }
        String res = studentService.adminGetOneStudent(admin,studentID,session);
        System.out.println(res);
        if(!res.equals("success")){
            return res;
        }
        return "success";
    }

    @ResponseBody
    @RequestMapping("admin-updateStudent.do")
    private String adminUpdateStudent(String studentID,String name,String department,String major,String stuClass,String password,
                                      String email,String phoneNumber,HttpSession session){
        System.out.println("update");
        Admin admin = (Admin)session.getAttribute("admin");
        if(admin == null){
            return "redirect:/adminLogin";
        }
        String res = studentService.adminUpdateStudent(admin,studentID,name,department,major,stuClass,password,email,phoneNumber,session);
        System.out.println(res);

        return res;
    }


    @RequestMapping("toAdmin-addStudents.do")
    public String toAdminAddStudents(HttpSession session){
        Admin admin = (Admin)session.getAttribute("admin");
        if(admin == null){
            return "redirect:/adminLogin";
        }
        String res = departmentService.getAllByUniversityID(admin.getUniversityID(),session);
        if(!res.equals("success")){
            return "404";
        }

        return "redirect:/admin-addStudents";
    }

    @ResponseBody
    @RequestMapping("admin-addOneStudent.do")
    public String adminAddOneStudent(String departmentID,String majorID,String stuClass,String studentID,String name,
                                     String gender,HttpSession session){

        Admin admin = (Admin)session.getAttribute("admin");
        if(admin == null){
            return "请先登录！";
        }

        StudentImpl  studentImpl = new StudentImpl();
        studentImpl.setStuClass(stuClass);
        studentImpl.setMajorID(majorID);
        studentImpl.setEbickAmount(0);
        studentImpl.setGender(gender);
        studentImpl.setUniversityID(admin.getUniversityID());
        studentImpl.setDepartmentID(departmentID);
        studentImpl.setName(name);
        studentImpl.setStudentID(studentID);

        String res = studentService.adminAddOneStudent(studentImpl,session);
        if(!res.equals("success")){
            return res;
        }


        return "success";

    }

    @RequestMapping("admin-getAllStudents.do")
    public String adminGetAllStudents(HttpSession session){

        Admin admin = (Admin)session.getAttribute("admin");
        if(admin == null){
            return "redirect:/adminLogin";
        }
        String res = studentService.adminGetAllStudents(admin,session);
        if(!res.equals("success")){
            return "404";
        }
        return "redirect:/admin-allStudents";

    }

    @ResponseBody
    @RequestMapping("deleteOneStudent.do")
    public String deleteOneStudent(HttpSession session,String studentID){

        System.out.println(studentID);
        Admin admin = (Admin)session.getAttribute("admin");
        if(admin == null){
            return "请先登录！";
        }
        String res = studentService.deleteOneStudent(admin,studentID);
        if(!res.equals("success")){
            return "删除失败！";
        }

        return "success";
    }




    /////////////////////////////////////////////////////////////////////////////
    @RequestMapping("getStudentInfo.do")
    public String getStudentInfo(HttpSession session){
        System.out.println(
                "getStudentInfo.do"
        );
        Student student = (Student)session.getAttribute("student");
        System.out.println(student);

        if(student == null)return "redirect:/";

        StudentImpl student1 = studentService.getStudentImpl(student);
        System.out.println(student1);
        session.setAttribute("studentInfo",student1);


        System.out.println(student1);
        System.out.println("return");
        return "redirect:/newStudentFinishInfo";
    }

    @ResponseBody
    @RequestMapping("finishStudentInfo.do")
    public String finishStudentInfo(@RequestParam("nickName")String nickName,
                                    @RequestParam("email")String email,
                                    @RequestParam("phoneNumber")String phoneNumber,
                                    MultipartFile image,
                                    HttpSession session){

        StudentImpl studentImpl = (StudentImpl)session.getAttribute("studentInfo");
        studentImpl.setNickName(nickName);
        studentImpl.setEmial(email);
        studentImpl.setPhoneNumber(phoneNumber);
        studentImpl.setImage(image.getOriginalFilename());
        System.out.println(studentImpl);

        String res = studentService.finishStudentInfo(studentImpl,image,session);

        if(!res.equals("success")){
            return "error";
        }
        return "success";
    }

}
